package com.lhd.ivos.vehicle.service.impl;

import com.lhd.ivos.vehicle.mapper.VehicleMapper;
import com.lhd.ivos.vehicle.pojo.dto.VehicleQuery;
import com.lhd.ivos.vehicle.pojo.dto.VehicleSaveParam;
import com.lhd.ivos.vehicle.pojo.entity.Vehicle;
import com.lhd.ivos.vehicle.pojo.vo.VehicleVO;
import com.lhd.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author lhd
 * date 2024/5/22
 * @apiNote
 */
@Slf4j
@Service
public class VehicleServiceImpl implements VehicleService{
    @Autowired
    VehicleMapper vehicleMapper;
    /**
     * 查询车辆信息列表
     *
     * @param vehicleQuery 车辆查询参数
     * @return 车辆信息VO列表
     */
    @Override
    public List<VehicleVO> selectVehicle(VehicleQuery vehicleQuery) {
        log.debug("查询车辆的条件为: {}", vehicleQuery);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        return list;
    }

    /**
     * 保存车辆信息
     * <p>
     * 如果车辆ID为null，则视为新建车辆，设置创建时间并插入数据库；
     * 否则，视为更新车辆信息，设置更新时间并执行更新操作。
     *
     * @param vehicleSaveParam 车辆保存参数
     */
    @Override
    public void save(VehicleSaveParam vehicleSaveParam) {
        log.debug("Saving vehicle: {}", vehicleSaveParam);
        Vehicle vehicle = new Vehicle();
        // 复制参数到Vehicle实体
        BeanUtils.copyProperties(vehicleSaveParam, vehicle);

        if (vehicle.getId() == null) {
            vehicle.setStatus("1");
            // 设置创建时间为当前时间
            vehicle.setCreateTime(new Date());
            // 插入新车辆记录
            vehicleMapper.insert(vehicle);
        } else {
            // 更新车辆时，设置更新时间为当前时间
            vehicle.setUpdateTime(new Date());
            // 更新车辆记录
            vehicleMapper.update(vehicle);
        }
    }

    /**
     * 根据ID删除车辆信息
     *
     * @param id 要删除的车辆ID
     */
    @Override
    public void delete(Long id) {
        log.debug("Deleting vehicle with ID: {}", id);
        // 删除指定ID的车辆记录
        vehicleMapper.delete(id);
    }

    @Override
    public void unbindGeofence(Long vehicleId) {
        log.debug("Unbinding geofence from vehicle with ID: {}", vehicleId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setGeofenceId(null);
        vehicle.setUpdateTime(new Date());
        //之前update方法有if test != null 要求是如果值为null 则不更新
        //但是现在的业务更新的值就是null值，需要给车辆解除绑定
        //所以需要单独设置一个mapper层的方法，给当前车辆geofence_id设为null
        vehicleMapper.updateNullValue(vehicle);

    }

    @Override
    public void bindGeofenceAndVehicle(Long geofenceId, Long vehicleId) {
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceId(geofenceId);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }
}
